import api from '../../utils/api'

// pages/comment/comment.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    interval: 5000,
    duration: 500,
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    comments: [],
    discussion: "",
    imgUrl: '',
    imgName: '',
    firstDay: "",
    inputVal: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideShareMenu()
    //保存数据
    this.setData({
      firstDay: options.firstDay
    })
    this.requestComments (0);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    var cur = e.target.dataset.current; //查询标题序号
    if (this.data.currentTaB == cur) {
      return false;
    } else {
      this.setData({
        currentTab: cur
      })
      this.requestComments (cur)
    }
  },
  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 1) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  // 上传图片
  uploadImg: function (e) {
    wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
            let imgUrl = res.tempFilePaths;
            //显示等待框
            wx.showLoading({
                title: '图片上传中',
            })
            //调取图片上传
            wx.uploadFile({
              filePath: res.tempFilePaths[0],
              name: 'img',
              url: api.uploadImage,
              success: (res) => {
                wx.hideLoading()
                var data = JSON.parse(res.data)
                if (data.state == 1) {
                  wx.showToast({
                    title: '图片上传成功',
                  })
                  this.setData({
                    //   imgUrl: data.data.imgUrl
                    imgUrl: imgUrl[0],
                    imgName: data.data.imgName
                  })
                } else if (data.state == "2") {
                  wx.showToast({
                    title: '图片上传失败',
                  })
                }
              },
              fail: function (err) {
                wx.showToast({
                    title: '图片上传失败',
                })
              },
              complete: () => {
                console.log("complete")
              }
            })
        }
    })
  },

  // 删除图片
  deleteImg: function (e) {
      this.setData({
          imgUrl: ''
      })
  },

  commentInput: function (e) {
    this.setData({
      discussion: e.detail.value
      // discussion:this.data.inputVal
    })

  },
  sendClick: function (e) {
    var that = this
    if (this.data.discussion == "") {
      wx.showToast({
        title: '请输入评论',
      })
    } else {
      wx.request({
        // url: 'https://542124141.xyz/pam/diss/add',
        url: api.sendDiscussion,
        method: "POST",
        data: {
          diss: that.data.discussion,
          imgName: that.data.imgName
        },
        header: {
          // 'content-type': 'application/x-www-form-urlencoded',
          'content-type': 'application/json',
          'token': wx.getStorageSync('token')
        },
        success(e) {
          wx.showToast({
            title: '评论成功！',
          })
          that.setData({
            inputVal: "",
            imgUrl: ''
          })
          that.requestComments (0)
        }
      })
    }
  },

  //请求数据
  requestComments: function (cur) {
    //清除数据
    this.setData({
      comments: new Array
    })
    var that = this
    let url
    if (cur == 0) {
      //计算月份
      var thisMonth = new Date().getMonth() + 1;
      this.setData({
        currentTab: cur,
        month: [thisMonth - 2, thisMonth - 1, thisMonth, thisMonth + 1, thisMonth + 2]
      })
      url = api.getDiscussion + "?begin_date=" + this.data.firstDay + "&pageSize=999"
    } else if (cur == 1) {
      url = api.getDiscussion + "?begin_date=2000-01-01&pageSize=999"
    }
    //请求评论
    wx.request({
      // url: "https://542124141.xyz/pam/diss/getList?begin_date=" + this.data.firstDay + "&pageSize=999",
      // url: api.getDiscussion + "?begin_date=" + this.data.firstDay + "&pageSize=999",
      url: url,
      method: "GET",
      success(res) {
        for (var i = 0; i < res.data.data.total; i++) {
          var userName = res.data.data.list[i].user_id
          var commentText = res.data.data.list[i].diss
          var commentTime = res.data.data.list[i].diss_time
          var replyName = "膳食委员会"
          var replyText = res.data.data.list[i].reply
          var replyTime = res.data.data.list[i].reply_time
          let imgUrl = res.data.data.list[i].imgUrl
          var tmp = {
            userName,
            commentTime,
            commentText,
            replyName,
            replyTime,
            replyText,
            imgUrl
          }
          that.data.comments.push(tmp)
        }
        that.setData({
          comments: that.data.comments
        })
      }
    })
  }
})